package cn.lghuntfor.commons.sms.tencent.config;

import cn.hutool.core.util.StrUtil;
import cn.lghuntfor.commons.sms.api.config.SmsProperties;
import cn.lghuntfor.commons.sms.api.service.InheritSmsService;
import cn.lghuntfor.commons.sms.tencent.service.impl.TencentSmsServiceImpl;
import com.tencentcloudapi.common.Credential;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 腾读短信配置
 * @author liaogang
 * @date 2021/2/1 15:39
 */
@Configuration
@EnableConfigurationProperties(value = SmsProperties.class)
public class TencentSmsConfig {

    @Autowired
    private SmsProperties smsProperties;

    @Value("${spring.sms.tencent.accessKey:}")
    private String accessKey;

    @Value("${spring.sms.tencent.accessSecret:}")
    private String accessSecret;

    @Bean
    public Credential credential() {
        if (StrUtil.isAllNotBlank(accessKey, accessSecret)) {
            return new Credential(accessKey, accessSecret);
        }
        return new Credential(smsProperties.getAccessKey(), smsProperties.getAccessSecret());
    }

    @Bean(name = "TENCENT")
    public InheritSmsService tencentSmsService() {
        return new TencentSmsServiceImpl();
    }

}
